package com.kamistoat.meimeimall.common.annotation.annotationHandler;

import com.kamistoat.meimeimall.common.annotation.AutoFillUpdateTime;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自动为数据库update操作填充 update_time 字段
 */
@Aspect
@Component
@Order(-50)
public class AutoFillUpdateTimeHandler {
    @Around(value = "@annotation(autoFillUpdateTime)")
    public Object innerAround(ProceedingJoinPoint proceedingJoinPoint, AutoFillUpdateTime autoFillUpdateTime) throws Throwable {
        // insert方法输入参数值
        Object paramValue = proceedingJoinPoint.getArgs()[0];
        // insert方法Entity向上找到BaseEntity
        Class<?> paramType = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterTypes()[0].newInstance().getClass();
        paramType.getMethod("setUpdateTime", Date.class).invoke(paramValue, new Date());
        return proceedingJoinPoint.proceed();
    }
}